<%@ page import="com.example.ordersystem.domain.Category" %>
<%@ page import="java.util.List" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSON" %><%--
  Created by IntelliJ IDEA.
  User: 檬zhu
  Date: 2021/12/15
  Time: 23:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/layui/css/layui.css">
</head>
<body>
<jsp:include page="title.jsp"/>
<table class="layui-table" style="width: 66%;margin-left: 15%">
    <thead>
    <tr>
        <th>分类编号</th>
        <th>分类名称</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <%
        if (request.getAttribute("error") != null) {
            out.println("<script language='JavaScript'>alert('无法删除分类,分类中仍包含菜品');window.location.href='/merchant/categoryPage'</script>");
            return;
        }

        if (request.getAttribute("success") != null) {
            out.println("<script language='JavaScript'>alert('删除分类成功');window.location.href='/merchant/categoryPage'</script>");
            return;
        }

        List<Category> categories;
        Object obj = request.getAttribute("categories");

        if (obj == null) { //会话过期
            response.sendRedirect("/");
            return; //不再执行接下来的代码
        } else {
            if (obj instanceof JSONArray) {
                categories = JSON.parseArray(((JSONArray) obj).toJSONString(), Category.class);
            } else {
                categories = (List<Category>) obj;
            }
            for (Category category : categories) {
    %>
    <tr>
        <td><%=category.getNum()%>
        </td>
        <td><%=category.getName()%>
        </td>
        <td>
            <form action="${pageContext.request.contextPath}/merchant/deleteCategory">
                <button onclick="return window.confirm('确认删除吗？')" class="layui-btn layui-btn-danger layui-btn-sm"
                        name="delete" value="删除">删除
                </button>
                <input type="hidden" name="categoryNum" value="<%=category.getNum()%>">
            </form>
        </td>
    </tr>
    <%
            }
        }
    %>
    </tbody>
</table>

<script src="${pageContext.request.contextPath}/resources/layui/layui.js"/>
<script>
    //JavaScript代码区域
    layui.use('table', function () {
        const table = layui.table;
    });
</script>
</body>
</html>
